<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="核销状态" prop="isVerify">
        <el-select v-model="queryParams.isVerify" placeholder="选择核销状态" style="width:140px;" size="mini">
          <el-option label="未核销" :value="0" />
          <el-option label="已核销" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="mini"
          @keyup.enter.native="handleQuery" style="width:140px;"/>
      </el-form-item>
      <el-form-item label="查询时间" prop="propertyName">
        <el-date-picker v-model="dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"-->
<!--          v-hasPermi="['module:productorder:remove']">删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
          v-hasPermi="['module:productorder:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" width="160"/>
<!--      <el-table-column label="所属商家" align="center" prop="shopName" />-->
      <el-table-column label="用户姓名" align="center" prop="realName" />
      <el-table-column label="用户电话" align="center" prop="userPhone" />
      <el-table-column label="核销码" align="center" prop="verifyCode"/>
      <el-table-column label="详细地址" align="center" prop="userAddress" />
      <el-table-column label="购买数量" align="center" prop="count"/>
      <el-table-column label="订单总价" align="center" prop="totalMoney">
        <template slot-scope="scope">{{scope.row.totalMoney}}元</template>
      </el-table-column>
      <el-table-column label="支付金额" align="center">
        <template slot-scope="scope">{{scope.row.payPrice}}元</template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>
            <!-- 订单状态 0 - 未支付   1 - 待支付  2 - 已支付  3 - 已核销 4 - 已评价
            5 - 订单取消  6 - 已申请退货  7 - 同意退货  8 - 退货完成 9 - 拒绝退货 10 - 已完成 -->
            <el-tag v-if="scope.row.status === 0" style="cursor: pointer" :type="''">未支付</el-tag>
            <el-tag v-if="scope.row.status === 1" style="cursor: pointer" :type="''">待支付</el-tag>
            <el-tag v-if="scope.row.status === 2" style="cursor: pointer" :type="'success'">已支付</el-tag>
            <el-tag v-if="scope.row.status === 3" style="cursor: pointer" :type="'success'">已核销</el-tag>
            <el-tag v-if="scope.row.status === 4" style="cursor: pointer" :type="''">已评价</el-tag>
            <el-tag v-if="scope.row.status === 5" style="cursor: pointer" :type="''">订单取消</el-tag>
            <el-tag v-if="scope.row.status === 6" style="cursor: pointer" :type="''">已申请退货</el-tag>
            <el-tag v-if="scope.row.status === 7" style="cursor: pointer" :type="''">同意退货</el-tag>
            <el-tag v-if="scope.row.status === 8" style="cursor: pointer" :type="''">退货完成</el-tag>
            <el-tag v-if="scope.row.status === 9" style="cursor: pointer" :type="''">拒绝退货</el-tag>
            <el-tag v-if="scope.row.status === 10" style="cursor: pointer" :type="''">已完成</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.isVerify === 0" @click="handleDelete(scope.row)">核销</el-button>
          <el-button type="success" v-if="scope.row.isVerify === 1">已核销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

  </div>
</template>

<script>
import { listOrder, listMerOrder,getOrder, delOrder,verifyOrder, addOrder, updateOrder, exportOrder } from "@/api/module/product/order";

export default {
  name: "Order",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      dateRange:[],
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isVerify:undefined,
        merId:undefined,
        orderNo: undefined,
        beginDate:undefined,
        endDate:undefined,
        orderType:0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listMerOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log('dateRange',this.dateRange);
      if(this.dateRange !=undefined && this.dateRange.length === 2){
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.beginDate = undefined;
      this.queryParams.endDate = undefined;
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认对当前订单进行核销?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return verifyOrder(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("核销成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
